package com.syni.mdd.sjb.common.datatreaure.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DankApplication;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DankHomePageStatistics;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DankMerchantsApplication;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.mapper.DankHomePageStatisticsMapper;
import com.syni.mdd.sjb.common.component.utils.common.utils.StringUtils;
import com.syni.mdd.sjb.common.component.utils.feign.service.mddAll.GetConfigService;
import com.syni.mdd.sjb.common.datatreaure.service.DankApplicationService;
import com.syni.mdd.sjb.common.datatreaure.service.DankHomePageStatisticsService;
import com.syni.mdd.sjb.common.datatreaure.service.DankMerchantsApplicationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页统计面板的相关值 服务实现类
 * </p>
 *
 * @author lcc
 * @since 2020-09-18
 */
@Service
public class DankHomePageStatisticsServiceImpl extends ServiceImpl<DankHomePageStatisticsMapper, DankHomePageStatistics> implements DankHomePageStatisticsService {

    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    GetConfigService getConfigService;
    @Autowired
    DankMerchantsApplicationService dankMerchantsApplicationService;
    @Autowired
    DankApplicationService dankApplicationService;

    private final Integer GET_DEFAULT_CONFIG = 106;


    @Override
    public void initMyService(Integer userId) {
        List<Integer> appIdList = new ArrayList<>();
        Map<String, Object> configMap = getConfigService.GetConfigInfo(GET_DEFAULT_CONFIG);
        log.info("configMap:{}",configMap);
        if(configMap != null && StringUtils.equals(configMap.get("status").toString(),"00")){
            String configStr = String.valueOf(configMap.get("data"));
            List<String> config = JSON.parseArray(configStr,String.class);
            for(String s : config){
                DankApplication appByName = dankApplicationService.getAppByName(s);
                appIdList.add(appByName.getId());
            }
        }
        DankMerchantsApplication dankMerchantsApplication = dankMerchantsApplicationService.findByUserId(userId);
        log.info("dankMerchantsApplication:{}",dankMerchantsApplication);
        if(dankMerchantsApplication == null){
            dankMerchantsApplication = new DankMerchantsApplication();
            dankMerchantsApplication.setAppId(JSON.toJSONString(appIdList));
            dankMerchantsApplication.setUserId(userId);
            dankMerchantsApplicationService.save(dankMerchantsApplication);
        }
    }
}
